const http = require('http');

// 测试不同的API端点
const tests = [
  {
    name: '获取所有产品 (无参数)',
    path: '/api/v1/inventory/products'
  },
  {
    name: '获取所有产品 (带分页参数)',
    path: '/api/v1/inventory/products?page=1&limit=20'
  },
  {
    name: '获取所有产品 (带空查询参数)',
    path: '/api/v1/inventory/products?page=1&limit=20&name=&sku=&status='
  }
];

function runTest(test, callback) {
  console.log(`\n=== ${test.name} ===`);
  
  const options = {
    hostname: 'localhost',
    port: 9001,
    path: test.path,
    method: 'GET'
  };

  const req = http.request(options, (res) => {
    console.log(`状态码: ${res.statusCode}`);
    
    let data = '';
    res.on('data', (chunk) => {
      data += chunk;
    });
    
    res.on('end', () => {
      try {
        const jsonData = JSON.parse(data);
        console.log(`总记录数: ${jsonData.data?.pagination?.total || 0}`);
        console.log(`当前页记录数: ${jsonData.data?.list?.length || 0}`);
        
        if (jsonData.data?.list?.length > 0) {
          console.log('前几条记录:');
          jsonData.data.list.slice(0, 3).forEach((product, index) => {
            console.log(`  ${index + 1}. ${product.sku} - ${product.name} (库存: ${product.stock})`);
          });
        } else {
          console.log('无数据返回');
        }
      } catch (error) {
        console.error('解析JSON时出错:', error.message);
        console.log('原始响应:', data.substring(0, 200) + '...');
      }
      
      callback();
    });
  });

  req.on('error', (error) => {
    console.error('请求出错:', error.message);
    callback();
  });

  req.end();
}

// 顺序执行所有测试
function runAllTests() {
  let index = 0;
  
  function nextTest() {
    if (index < tests.length) {
      runTest(tests[index], () => {
        index++;
        setTimeout(nextTest, 1000); // 间隔1秒执行下一个测试
      });
    }
  }
  
  nextTest();
}

// 运行测试
runAllTests();